#include "engctrlpanel.h"
#include "qpainter.h"

EngCtrlPanel::EngCtrlPanel(QWidget* parent):
    QWidget(parent)
{

}

void EngCtrlPanel::paintEvent(QPaintEvent *)
{
    int width = this->width();
    int height = this->height();
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width/2, height/2);
    int side = qMin(width, height);
    painter.scale(side / 200.0, side / 200.0);

    drawThrRing(&painter);
    drawMixRing(&painter);
    drawSltRing(&painter);
    drawThrText(&painter);
}

void EngCtrlPanel::drawThrRing(QPainter *painter)
{
    float value = thr;
    float angle = value/100*360;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(49,54,62,255));
    painter->drawPie(-100,-100,200,200,(90-angle)*16,360*16);

    painter->setBrush(QColor(157,195,230,255));
    painter->drawPie(-100,-100,200,200,(90-angle)*16,angle*16);
    painter->setBrush(QColor(34,42,53,255));
    painter->drawPie(-90,-90,180,180,(90-angle)*16,360*16);
    painter->restore();
}

void EngCtrlPanel::drawMixRing(QPainter *painter)
{
    float value = mix;
    float angle = value/100*360;
    painter->save();
    QPen pen;
    pen.setColor(QColor(0,255,0,255));
    pen.setWidthF(1.0);
    painter->setPen(Qt::NoPen);
    //阴影圈
    painter->setBrush(QColor(49,54,62,255));
    painter->drawPie(-85,-85,170,170,90*16,360*16);
    //进度条
    if(value >= 100)
        painter->setPen(pen);
    else
        painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(244,177,131,255));
    painter->drawPie(-85,-85,170,170,(90-angle)*16,angle*16);
    //内圈遮挡圆
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(34,42,53,255));
    painter->drawPie(-75,-75,150,150,90*16,360*16);
    painter->restore();
}

void EngCtrlPanel::drawSltRing(QPainter *painter)
{
    float value = slt;
    float angle = value/100*360;
    painter->save();
    painter->setPen(Qt::NoPen);
    //阴影圈
    painter->setBrush(QColor(49,54,62,255));
    painter->drawPie(-70,-70,140,140,90*16,360*16);
    //进度条
    painter->setBrush(QColor(145,72,200,255));
    painter->drawPie(-70,-70,140,140,(90-angle)*16,angle*16);
    //内圈遮挡圆
    painter->setBrush(QColor(34,42,53,255));
    painter->drawPie(-60,-60,120,120,90*16,360*16);
    painter->restore();
}

void EngCtrlPanel::drawThrText(QPainter *painter)
{
    painter->save();
    QPen pen;
    pen.setColor(QColor(157,195,230,255));
    pen.setWidthF(1.0);
    painter->setPen(pen);
    QFont font = painter->font();
    font.setPixelSize(20);
    font.setFamily("consolas");
    painter->setFont(font);
    painter->drawText(-60,-45,120,40,Qt::AlignCenter,QString("THR %1%").arg(thr,0,'f',0),Q_NULLPTR);

    pen.setColor(QColor(244,177,131,255));
    painter->setPen(pen);
    painter->drawText(-60,-20,120,40,Qt::AlignCenter,QString("MIX %1%").arg(mix,0,'f',0),Q_NULLPTR);

    pen.setColor(QColor(145,72,200,255));
    painter->setPen(pen);
    painter->drawText(-60,5,120,40,Qt::AlignCenter,QString("SLT %1%").arg(slt,0,'f',0),Q_NULLPTR);

    painter->restore();
}



void EngCtrlPanel::setEngValue(float thr,float mix,float slt)
{
    this->thr = thr;
    this->mix = mix;
    this->slt = slt;
}
float EngCtrlPanel::getThrValue()
{
    return this->thr;
}
float EngCtrlPanel::getMixValue()
{
    return this->mix;
}
float EngCtrlPanel::getSltValue()
{
    return this->slt;
}

